package com.yuanzi.base.websocket;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.yuanzi.base.constants.BaseConstants;
import com.yuanzi.base.redis.OuterStringRedisService;
import com.yuanzi.base.util.StringUtils;

/**
 * websocket长连接
 * @author chengshx
 * @date 2017年11月25日
 */
@Service
public class MessageSenderService {
	
	private final static String SERVICE_NAME = "websocket-service";
	private final static String WEBSOCKET_PORT = ":8899";
	private final static String MEMBER_METHOD = "/msg.member.send";
	private final static String CLASS_METHOD = "/msg.class.send";
	
//	@Autowired
//	private DiscoveryClient discoveryClient;
	@Autowired
	private RestTemplate restTemplate;
	
	/**
	 * 发送消息给班级所有的成员
	 * @author chengshx
	 * @date 2017年11月26日
	 *
	 * @param teachClassId
	 * @param msg
	 */
//	public void sendMessage2Class(Long teachClassId, String msg) {
//		List<String> hosts = getWebsocketServiceHosts();
//		for(String host : hosts) {
//			String url = BaseConstants.HTTP_PREFIX + host + WEBSOCKET_PORT + CLASS_METHOD;
//			HttpHeaders headers = new HttpHeaders();
//			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//			MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
//			map.add("teachClassId", StringUtils.long2Str(teachClassId));
//			map.add("msg", msg);
//			HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
//			restTemplate.postForObject(url, request, String.class);
//		}
//	}
	
	/**
	 * 发送消息给班级的成员
	 * @author chengshx
	 * @date 2017年11月26日
	 *
	 * @param teachClassId
	 * @param memberId
	 * @param msg
	 */
	public void sendMessage2Member(Long teachClassId, Long memberId, String msg) {
		String host = getMemberHost(teachClassId, memberId);
		if(host != null) {
			String url = BaseConstants.HTTP_PREFIX + host + WEBSOCKET_PORT + MEMBER_METHOD;
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
			MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
			map.add("teachClassId", StringUtils.long2Str(teachClassId));
			map.add("memberId", StringUtils.long2Str(memberId));
			map.add("msg", msg);
			HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
			restTemplate.postForObject(url, request, String.class);
		}
	}
	
	/**
	 * 获取websocket的所有实例Host
	 * @author chengshx
	 * @date 2017年11月26日
	 *
	 * @return
	 */
//	private List<String> getWebsocketServiceHosts() {
//		List<String> hosts = new ArrayList<>();
//		List<ServiceInstance> list = discoveryClient.getInstances(SERVICE_NAME);
//		if(list != null && list.size() > 0) {
//			for (ServiceInstance serviceInstance : list) {
//				hosts.add(serviceInstance.getHost());
//			}
//		}
//		return hosts;
//	}
	
	/**
	 * 获取用户连接的Host
	 * @author chengshx
	 * @date 2017年11月26日
	 *
	 * @param teachClassId
	 * @param memberId
	 * @return
	 */
	private String getMemberHost(Long teachClassId, Long memberId) {
		return OuterStringRedisService.get(SERVICE_NAME, BaseConstants.WEBSOCKET, teachClassId + ":" + memberId);
	}
	
}
